#include "e7convert.h"

/** Universal utility routines for type convertions
 *  from unix-v7 floppie to local gnu-c++
 */

/* NB:
 * blok = array van bytes (b.v. een diskblok),
 * offset = positie in het blok
 */

// a 16 bit short int
short getshort(byte blok[], int offset) {
	unsigned int b1 = 0xFF & blok[offset++];
	unsigned int b2 = 0xFF & blok[offset];
	return (short) ((b1 << 8) + b2);
}

// a 24 bit half-long int
int getlong3(byte blok[], int offset) {
	int b1 = 0xFF & blok[offset++];
	int b2 = 0xFF & blok[offset++];
	int b3 = 0xFF & blok[offset];
	return (b1 << 16) + (b2 << 8) + b3;
}

// a 32 bit long int
long getlong(byte blok[], int offset) {
	return getlong4(blok, offset);
}

// intern: 4-byte long/int ophalen
long getlong4(byte blok[], int offset) {
	int b1 = 0xFF & blok[offset++];
	int b2 = 0xFF & blok[offset++];
	int b3 = 0xFF & blok[offset++];
	int b4 = 0xFF & blok[offset];
	return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
}

// een reeks van 'count' ascii characters
string getchars(byte blok[], int offset, int count) {
	string s = "";
	for (int i = 0; (i < count) && (blok[offset] != 0); ++i) {
		s += ((char) (blok[offset++]));
	}
	return s;
}
